package boddaert.philippe.cashGame.site.priceminister;

import java.util.List;

import org.apache.log4j.Logger;

import au.id.jericho.lib.html.Element;
import boddaert.philippe.cashGame.GameConfiguration;
import boddaert.philippe.cashGame.GameConfigurationCurrencyType;

public class WebGameParser extends
		boddaert.philippe.cashGame.parser.game.WebGameParser {

	private final static Logger logger = Logger.getLogger(WebGameParser.class);

	protected int parseSecondHandAvailable(Element element) {
		List<Element> title = element.findAllElements("li");
		for (Element element2 : title) {
			String numberArticle = element2.getContent().toString();
			if ("nb_products".equals(element2.getAttributeValue("class"))
					&& numberArticle.contains("Nombre d'articles")) {
				int indexOfOccasion = numberArticle.indexOf("occasion");
				if (indexOfOccasion > -1) {
					int beginEm = numberArticle.substring(0, indexOfOccasion)
							.lastIndexOf("<em>");
					int endEm = numberArticle.substring(beginEm,
							indexOfOccasion).indexOf("</em>");
					return Integer.parseInt(numberArticle.substring(
							beginEm + "<em>".length(), beginEm + endEm).trim());
				}
			}
		}
		return -1;
	}

	protected int parseNewAvailable(Element element) {
		List<Element> title = element.findAllElements("li");
		for (Element element2 : title) {
			String numberArticle = element2.getContent().toString();
			if ("nb_products".equals(element2.getAttributeValue("class"))
					&& numberArticle.contains("Nombre d'articles")) {
				int indexOfOccasion = numberArticle.indexOf("neuf");
				if (indexOfOccasion > -1) {
					int beginEm = numberArticle.substring(0, indexOfOccasion)
							.lastIndexOf("<em>");
					int endEm = numberArticle.substring(beginEm,
							indexOfOccasion).indexOf("</em>");
					return Integer.parseInt(numberArticle.substring(
							beginEm + "<em>".length(), beginEm + endEm).trim());
				}
			}
		}
		return -1;
	}

	protected double parseBestPrice(Element element) {
		List<Element> title = element.findAllElements("span");
		for (Element element2 : title) {
			if ("advtype advtype_used".equals(element2
					.getAttributeValue("class"))) {
				logger.info("parse the best price : "
						+ element2.getContent().toString().trim());
				String[] split = element2.getContent().toString().split(
						"&nbsp;&euro");
				return Double.parseDouble(split[0].trim().replaceAll(",", "."));
			}
		}
		return -1;
	}

	protected String parseTitle(Element element) {
		List<Element> title = element.findAllElements("h3");
		Element link = (Element) title.get(0).getChildElements().get(0);
		logger.info("parse the game title : " + link.getContent().toString());
		return link.getContent().toString();
	}

	protected String parseUrl(Element element) {
		List<Element> title = element.findAllElements("h3");
		Element link = (Element) title.get(0).getChildElements().get(0);
		return "http://www.priceminister.com" + link.getAttributeValue("href");
	}

	@Override
	protected GameConfiguration getNewGameConfiguration() {
		return new GameConfiguration(PriceMinister.NAME,
				GameConfigurationCurrencyType.EURO);
	}
}
